<?php

# http://projects:8081/cdapp-2kt-api/api/cem_sector_set.php

include_once '_portal.php';

if (!$post ||
  !$post->title ||
  !$post->modes ||
  !$post->areas) {
  output(["success" => false, 'post' => $post]);
  die();
} else {
  $number = $post->number;
  $title = $post->title;
  $description = $post->description;
  $modes = json_stringify($post->modes);
  $areas = json_stringify($post->areas);
  $areaCount = count($post->areas);
  $clientIP = get_client_seat(get_client_ip());

  if ($number !== -1) {
    $sql = "UPDATE `2kt_cem_sectors` SET `status`= 0 WHERE `number` = {$number}";
    exec_sql($sql);
  }
  $sql =
    "INSERT INTO `2kt_cem_sectors`(`title`, `description`, `modes`, `areas`, `areaCount`, `clientIP`) VALUES ('{$title}','{$description}','{$modes}','{$areas}','{$areaCount}','{$clientIP}')";

  $result = exec_sql($sql);
  output([
    "success" => $result,
    'sql' => $sql,
    'post' => $post,
  ]);
  die();
}